package com.invite.me.ui.yaoyue;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alipay.sdk.app.PayTask;
import com.invite.me.R;
import com.invite.me.base.BaseActivity;
import com.invite.me.beans.presenter.InviteRecordDetailPresenter;
import com.invite.me.beans.res.InviteRecordDetailByXqShouBean;
import com.invite.me.beans.res.InviteRecordDetailZfbBeanShou;
import com.invite.me.ui.pay.PayResult;
import com.invite.me.util.ToastUtils;
import com.invite.me.wlqq.Contract;

import java.util.Map;

public class InviteRecordDetailActivity extends BaseActivity implements View.OnClickListener,Contract.BaseView{

    String fupricetype="qianbao";

    private static final int SDK_PAY_FLAG = 1;
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     * 对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        Log.e("dingdan", "该笔订单是否真实支付成功，需要依赖服务端的异步通知。" );
                        inviteRecordDetailPresenter.ChakanXiangqing(qianyiyeid);

                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        Log.e("dingdan", "该笔订单真实的支付结果，需要依赖服务端的异步通知。" );
                        ToastUtils.show(InviteRecordDetailActivity.this,"支付取消");
                    }
                    break;
                }

                default:
                    break;
            }
        };
    };
    InviteRecordDetailPresenter inviteRecordDetailPresenter;
    private LinearLayout fuyuechenggong;
    private LinearLayout quxiaoyuanyin;
    private LinearLayout yaoyuezhedaitongyi;
    private LinearLayout tongyifuyue;
    private LinearLayout fuyuzhongnvlist;
    private TextView quzhifu;
    private TextView quxiaoyaoyue;
    private TextView tijiao;
    private TextView fuyuedizhi;
    private TextView priceyh;
    private String xiangqingid;
    private RelativeLayout fuyuezhong;
    private RelativeLayout yifuyue;
    private RelativeLayout yiwancheng;
    private RelativeLayout meifuyue;
    private TextView quxiaoyuanyintext;
    private ImageView image_fanhui;
    private double value;
    private String qianyiyeid;


    @Override
    protected void initView() {
        inviteRecordDetailPresenter=new InviteRecordDetailPresenter(this);
        Intent intent = getIntent();
        String leixing = intent.getStringExtra("state");
        qianyiyeid = intent.getStringExtra("id");

        inviteRecordDetailPresenter.ChakanXiangqing(qianyiyeid);

        //中部东西
        fuyuedizhi = findViewById(R.id.fuyuedizhi);
        priceyh = findViewById(R.id.priceyh);
        image_fanhui = findViewById(R.id.image_fanhui);


        //邀约状态
        meifuyue = findViewById(R.id.meifuyue);
        fuyuezhong = findViewById(R.id.fuyuezhong);
        yifuyue = findViewById(R.id.yifuyue);
        yiwancheng = findViewById(R.id.yiwancheng);


        //已邀约
        yaoyuezhedaitongyi = findViewById(R.id.yaoyuezhedaitongyi);
        //去支付
        tongyifuyue = findViewById(R.id.tongyifuyue);
        quzhifu = findViewById(R.id.quzhifu);
        //取消邀约
        fuyuzhongnvlist = findViewById(R.id.fuyuzhongnvlist);
        quxiaoyaoyue = findViewById(R.id.quxiaoyaoyue);
        //提交
        fuyuechenggong = findViewById(R.id.fuyuechenggong);
        tijiao = findViewById(R.id.tijiao);
        //投诉和意见
        quxiaoyuanyin = findViewById(R.id.quxiaoyuanyin);
        quxiaoyuanyintext = findViewById(R.id.quxiaoyuanyintext);

        quzhifu.setOnClickListener(this);
        quxiaoyaoyue.setOnClickListener(this);
        tijiao.setOnClickListener(this);
        image_fanhui.setOnClickListener(this);





    }

    @Override
    protected int getActivityLayoutId() {
        // return R.layout.activity_invite_record;
        return R.layout.activity_invityrecord;
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.quxiaoyaoyue:
                yaoyuepop();
                break;
            case R.id.image_fanhui:
                finish();
                break;
            case R.id.quzhifu:
                //去支付
                zhifupop(xiangqingid,value);

                break;
        }

    }

    @Override
    public void setPresenter(Contract.BasePresenter basePresenter) {
        if (inviteRecordDetailPresenter instanceof Contract.BasePresenter) {
            this.inviteRecordDetailPresenter = (InviteRecordDetailPresenter) basePresenter;
        }
    }

    @Override
    public void onDataSucess(Object o){
        if(o instanceof InviteRecordDetailByXqShouBean){
            InviteRecordDetailByXqShouBean inviteRecordDetailByXqShouBean= (InviteRecordDetailByXqShouBean) o;

            if(inviteRecordDetailByXqShouBean.getCode()==1000){
                value = inviteRecordDetailByXqShouBean.getData().getValue();
                String address = inviteRecordDetailByXqShouBean.getData().getAddress();
                xiangqingid = inviteRecordDetailByXqShouBean.getData().getId();
                fuyuedizhi.setText("赴约地址："+address);
                priceyh.setText(value +"");


                String state = inviteRecordDetailByXqShouBean.getData().getState();
                if(!TextUtils.isEmpty(state)){
                    if(state.equals("0")){
                        //未支付
                        fuyuezhong.setVisibility(View.VISIBLE);
                        yaoyuezhedaitongyi.setVisibility(View.VISIBLE);

                    }else if(state.equals("1")){
                        //要约接受着已确认
                        yifuyue.setVisibility(View.VISIBLE);
                        tongyifuyue.setVisibility(View.VISIBLE);
                        quzhifu.setText("去支付 ¥"+ value);
                    }else if(state.equals("2")){
                        //已支付
                        tongyifuyue.setVisibility(View.GONE);
                        yifuyue.setVisibility(View.VISIBLE);
                        fuyuzhongnvlist.setVisibility(View.VISIBLE);

                    }else if(state.equals("3")){
                        //已完成
                        fuyuechenggong.setVisibility(View.VISIBLE);
                        yiwancheng.setVisibility(View.VISIBLE);

                    }else if(state.equals("4")){
                        //已支付佣金
                        fuyuechenggong.setVisibility(View.VISIBLE);
                        yiwancheng.setVisibility(View.VISIBLE);
                    }else if(state.equals("50")){
                        //已退款
                        meifuyue.setVisibility(View.VISIBLE);
                        quxiaoyuanyin.setVisibility(View.VISIBLE);
                        quxiaoyuanyintext.setText("");

                    }else if(state.equals("100")){
                        //已取消
                        meifuyue.setVisibility(View.VISIBLE);
                        quxiaoyuanyin.setVisibility(View.VISIBLE);
                        quxiaoyuanyintext.setText("");
                    }else if(state.equals("101")){
                        //邀约者已取消
                        quxiaoyuanyin.setVisibility(View.VISIBLE);
                        quxiaoyuanyintext.setText("");
                    }else if(state.equals("102")){
                        //接受者已取消
                        quxiaoyuanyin.setVisibility(View.VISIBLE);
                        quxiaoyuanyintext.setText("");
                    }



                }




            }


        }else if(o instanceof InviteRecordDetailZfbBeanShou){
            InviteRecordDetailZfbBeanShou inviteRecordDetailZfbBeanShou= (InviteRecordDetailZfbBeanShou) o;
            final String zifuchuan = inviteRecordDetailZfbBeanShou.getZifuchuan();
            Runnable payRunnable = new Runnable() {

                @Override
                public void run() {
                    PayTask alipay = new PayTask(InviteRecordDetailActivity.this);
                    Map<String,String> result = alipay.payV2(zifuchuan,true);

                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = result;
                    mHandler.sendMessage(msg);
                }
            };
            // 必须异步调用
            Thread payThread = new Thread(payRunnable);
            payThread.start();


        }

    }

    @Override
    public void onDataFaile(Object o) {

    }

    @Override
    public void onComplete() {

    }

    public void yaoyuepop() {
        View parent = ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
        View view = LayoutInflater.from(this).inflate(R.layout.inviterecordqx_pop, null, false);
        TextView quxiao=view.findViewById(R.id.quxiao);
        TextView queding=view.findViewById(R.id.queding);


        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setFocusable(true);
        //产生背景变暗效果
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.4f;
        getWindow().setAttributes(lp);
        popupWindow.setOutsideTouchable(true);// 设置同意在外点击消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        popupWindow.setAnimationStyle(R.style.anim_menu_bottombar);

        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });




        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            //在dismiss中恢复透明度
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });

        popupWindow.showAtLocation(parent, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);


    }

    public void zhifupop(final String zhifuid,Double qian){

        View parent = ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
        View view = LayoutInflater.from(this).inflate(R.layout.yaoyuepirce_pop, null, false);
        TextView qiannn = view.findViewById(R.id.qian);
        qiannn.setText(qian+"");
        RelativeLayout zhifubao = view.findViewById(R.id.zhifubao);
        RelativeLayout qianbao = view.findViewById(R.id.qianbao);
        ImageView quxiao=view.findViewById(R.id.quxiao);
        final ImageView qianbaoduigou=view.findViewById(R.id.qianbaoduigou);
        final ImageView zhifubaoduigou=view.findViewById(R.id.zhifubaoduigou);
        TextView quzhifu=view.findViewById(R.id.quzhifu);

        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setFocusable(true);
        //产生背景变暗效果
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.4f;
        getWindow().setAttributes(lp);
        popupWindow.setOutsideTouchable(true);// 设置同意在外点击消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        popupWindow.setAnimationStyle(R.style.anim_menu_bottombar);

        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        qianbao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fupricetype="qianbao";
                qianbaoduigou.setVisibility(View.VISIBLE);
                zhifubaoduigou.setVisibility(View.GONE);

            }
        });
        zhifubao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fupricetype="zhifubao";
                qianbaoduigou.setVisibility(View.GONE);
                zhifubaoduigou.setVisibility(View.VISIBLE);
            }
        });
        quzhifu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(fupricetype.equals("qianbao")){
                    ToastUtils.show(InviteRecordDetailActivity.this,"钱包支付");
                    popupWindow.dismiss();

                } else if(fupricetype.equals("zhifubao")){
                    inviteRecordDetailPresenter.ZhiFuBaoZhiFu(zhifuid);
                    //  ToastUtils.show(InviteRecordDetailActivity.this,"支付宝支付");
                    popupWindow.dismiss();
                }

            }
        });



        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            //在dismiss中恢复透明度
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });

        popupWindow.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);



    }

}
